<?php
App::uses('AppModel', 'Model');

class Curriculo extends AppModel {
  public $useTable = 'curriculos';

  public $belongsTo = array(
    'User' => array(
      'className' => 'User',
      'foreignKey' => 'user_id'
    )
  );

  public $hasOne = array(
  'ExpArea' => array(
    'className' => 'ExpArea',
    'foreignKey' => 'curriculo_id',
  ),
  'ExpGenero' => array(
    'className' => 'ExpGenero',
    'foreignKey' => 'curriculo_id',
  ),
  'ObsAtor' => array(
    'className' => 'ObsAtor',
    'foreignKey' => 'curriculo_id',
  ),
  'ObsLocutor' => array(
    'className' => 'ObsLocutor',
    'foreignKey' => 'curriculo_id',
  ));

  public $hasMany = array(
    'Image' => array(
      'className' => 'Attachment',
      'foreignKey' => 'foreign_key',
      'conditions' => array(
        'Image.model' => 'Curriculo'
      ),
    ),
    'Funcao' => array(
      'className' => 'Funcao',
      'foreignKey' => 'curriculo_id',
    ),
    'Formacao' => array(
      'className' => 'Formacao',
      'foreignKey' => 'curriculo_id',
    ),
    'Curso' => array(
      'className' => 'Curso',
      'foreignKey' => 'curriculo_id',
    ),
    'Experiencia' => array(
      'className' => 'Experiencia',
      'foreignKey' => 'curriculo_id',
    ),
    'Portfolio' => array(
      'className' => 'Portfolio',
      'foreignKey' => 'curriculo_id',
    )
  );



  public function createWithAttachments($data)
  {

    // Sanitize your images before adding them

    $images = array();
    if (!empty($data['Image'][0]['attachment']['name'])) {
      foreach ($data['Image'] as $i => $image) {
        if (is_array($data['Image'][$i])) {

          // Force setting the `model` field to this model
          $image['model'] = 'Curriculo';
          $ext = substr($image['attachment']['name'], -4);
          $newName = inflector::slug($data['User']['fullname']);
          $image['attachment']['name'] = $newName. $ext;


          // Unset the foreign_key if the user tries to specify it
          if (isset($image['foreign_key'])) {
            //unset($image['foreign_key']);
          }

          $images[] = $image;
        }
      }
    }
    $data['Image'] = $images;



    foreach($data['Image'] as $key=>$dataImage)
    {
      /*if(!empty($data['Curriculo']['id'])):
        $data['Image'][$key]['foreign_key'] = $data['Curriculo']['id'];
      endif;*/
      //die(debug($key));
      if(isset($data['Curriculo']['id']) && !empty($data['Image'][$key]))
      {
        //die(debug($data));
        $imageBefore = $this->Image->find('first', array('conditions'=>array('Image.model'=>'Curriculo', 'Image.foreign_key'=>$data['Curriculo']['id'])));
        if(!empty($imageBefore))
        $this->Image->delete($imageBefore['Image']['id']);
      }
    }


    // Try to save the data using Model::saveAll()

    $this->create();
    if ($this->save($data)) {
      foreach($data['Image'] as $key=>$dataImage)
      {
        if(!empty($data['Curriculo']['id'])):
          $data['Image'][$key]['foreign_key'] = $data['Curriculo']['id'];
        else:
          $data['Image'][$key]['foreign_key'] = $this->getLastInsertId();
        endif;
      }
      if (!empty($data['Image'][0]['attachment']['name'])) {
        //die(debug($data));
        $this->Image->save($data['Image'][0]);
      }
      return true;
    }

    // Throw an exception for the controller
    throw new Exception(__("This post could not be saved. Please try again"));

  }

}
